﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Controls;

namespace ResourceBlender.Express {
    /// <summary>
    /// A button which displays a menu when clicked
    /// </summary>
    public class MenuButton : ToggleButton {
        public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(MenuButton), new UIPropertyMetadata(null));
        /// <summary>
        /// Gets or sets the menu to display when the button is clicked.
        /// </summary>
        /// <value>The menu.</value>
        public ContextMenu Menu {
            get { return (ContextMenu)base.GetValue(MenuProperty); }
            set { base.SetValue(MenuProperty, value); }
        }

        public static readonly DependencyProperty MenuLocationProperty = DependencyProperty.Register("MenuLocation", typeof(PlacementMode), typeof(MenuButton), new UIPropertyMetadata(PlacementMode.Bottom));
        /// <summary>
        /// Gets or sets the position of the menu in relation to the main button.
        /// </summary>
        /// <value>The menu location.</value>
        public PlacementMode MenuLocation {
            get { return (PlacementMode)base.GetValue(MenuLocationProperty); }
            set { base.SetValue(MenuLocationProperty, value); }
        }

        public static readonly DependencyProperty ShowArrowProperty = DependencyProperty.Register("ShowArrow", typeof(bool), typeof(MenuButton), new UIPropertyMetadata(true));
        /// <summary>
        /// Gets or sets whether to show the direction arrow
        /// </summary>
        /// <value><c>true</c> if the arrow should be shown, otherwise <c>false</c>.</value>
        public bool ShowArrow {
            get { return (bool)base.GetValue(ShowArrowProperty); }
            set { base.SetValue(ShowArrowProperty, value); }
        }

        public static readonly DependencyProperty AlwaysShowBorderProperty = DependencyProperty.Register("AlwaysShowBorder", typeof(bool), typeof(MenuButton), new UIPropertyMetadata(false));
        /// <summary>
        /// Gets or sets whether to always show the border.
        /// </summary>
        /// <value><c>true</c> if the border should be shown, otherwise <c>false</c>.</value>
        public bool AlwaysShowBorder {
            get { return (bool)base.GetValue(AlwaysShowBorderProperty); }
            set { base.SetValue(AlwaysShowBorderProperty, value); }
        }

        /// <summary>
        /// Initializes a new <see cref="MenuButton"/>.
        /// </summary>
        public MenuButton() {
            Binding binding = new Binding("Menu.IsOpen");
            binding.Source = this;
            this.SetBinding(IsCheckedProperty, binding);
        }

        /// <summary>
        /// Shows the menu when the control is clicked
        /// </summary>
        protected override void OnClick() {
            if(Menu != null) {
                Menu.PlacementTarget = this;
                Menu.Placement = MenuLocation;
                Menu.IsOpen = true;
            }
        }
    }
}
